package cn.strive.eams.user.controller;

import cn.strive.eams.base.response.JsonResult;
import cn.strive.eams.user.pojo.dto.*;
import cn.strive.eams.user.pojo.vo.UserVO;
import cn.strive.eams.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/v1/user")
@Api(tags = "用户管理")
@Slf4j
public class UserController {
    @Autowired
    UserService userService;

    @PostMapping("/login")
    @ApiOperation(value = "用户登录")
    public JsonResult login(UserLoginParam userLoginParam) {
        log.info("用户登录:userLoginParam={}", userLoginParam);
        // 调用service层 处理用户登录 Alt+Enter
        // Alt+Enter调用方法 Ctrl去接口 Ctrl+Alt去接口实现类
        UserVO userVO = userService.login(userLoginParam);
        return JsonResult.ok(userVO);
    }

    @PostMapping("/backup")
    @ApiOperation(value = "根据电话登录")
    public JsonResult backup(UserLoginParam userLoginParam) {
        UserVO userVO = userService.backup(userLoginParam);
        return JsonResult.ok(userVO);
    }

    @GetMapping("/select")
    @ApiOperation(value = "查询员工信息")
    public JsonResult selectAll(UserQuery userQuery){
        log.info("查询员工信息:employeeQuery={}", userQuery);
        List<UserVO> list = userService.selectAll(userQuery);
        return JsonResult.ok(list);
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存员工信息")
    public JsonResult save(UserSaveParam userSaveParam){
        log.info("保存员工信息:userSaveParam={}", userSaveParam);
        userService.saveInfo(userSaveParam);
        return JsonResult.ok();
    }

    @PutMapping("/savePassword")
    @ApiOperation(value = "修改密码")
    public JsonResult updateSavePassword(UserSavePassword userSavePassword){
        log.info("修改密码:employeeSavePassword={}", userSavePassword);
        userService.savePassword(userSavePassword);
        return JsonResult.ok();
    }

    @PutMapping("/update/status/{id}/{status}")
    @ApiOperation(value = "更新员工状态")
    public JsonResult updateStatus(@PathVariable Long id, @PathVariable String status){
        log.info("更新员工状态:id={},status={}", id, status);
        userService.updateStatus(id, status);
        return JsonResult.ok();
    }

    @PutMapping("/update/password/{id}")
    @ApiOperation(value = "重置员工密码")
    public JsonResult updatePassword(@PathVariable Long id){
        log.info("重置员工密码:id={}", id);
        userService.updatePassword(id);
        return JsonResult.ok();
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除员工")
    public JsonResult delete(@PathVariable Long id){
        log.info("删除员工信息:id={}", id);
        userService.delete(id);
        return JsonResult.ok();
    }

    @DeleteMapping("/delete/batch/{ids}")
    @ApiOperation(value = "批量删除员工")
    public JsonResult deleteBatch(@PathVariable List<Long> ids){
        log.info("批量删除员工信息:ids={}", ids);
        userService.deleteBatch(ids);
        return JsonResult.ok();
    }

    @GetMapping("/select/leader")
    @ApiOperation(value = "查询领导信息")
    public JsonResult selectLeader(LeaderQuery leaderQuery){
        log.info("查询领导信息:selectLeader={}",leaderQuery);
        List<UserVO> list = userService.selectLeader(leaderQuery);
        return  JsonResult.ok(list);
    }

    @GetMapping("/select/audit/{leader}")
    @ApiOperation(value = "查询审批领导信息")
    public JsonResult selectAudit(@PathVariable Long leader){
        log.info("查询审批领导信息:leader={}",leader);
        List<UserVO> list = userService.selectAudit(leader);
        return  JsonResult.ok(list);
    }

    @GetMapping("/select/allocation/{role}")
    @ApiOperation(value = "查询最后是谁干活的信息")
    public JsonResult selectAllocation(@PathVariable Long role){
        log.info("查询最后是谁干活的信息:role={}",role);
        List<UserVO> list = userService.selectAllocation(role);
        return  JsonResult.ok(list);
    }

    @GetMapping("/select/drivers")
    @ApiOperation(value = "查询司机信息")
    public JsonResult selectDrivers(){
        log.info("查询司机信息");
        List<UserVO> list = userService.selectDrivers();
        return  JsonResult.ok(list);
    }
}
